package com.lamatek.tags.google.beans;

import java.io.Serializable;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

/**
 * EscapeChars
 * @author Tom Cole
 * @version 0.40
 * 
 * This class simply escapes HTML content so it renders properly
 * when included in the body of a GoogleMapInfoWidowTag (&lt;googlemaps:infowindow>).
 * 
 */
public final class EscapeChars implements Serializable {

  /**
   * Accepts a string and returns an escaped string for use
   * within a GoogleMapInfoWindowTag (&lt;googlemaps:infowindow>).
   * 
   * @param text The original HTML text.
   * @return An escaped version of the original text.
   */
  public static String escape(String text){
    final StringBuffer result = new StringBuffer();
    final StringCharacterIterator iterator = new StringCharacterIterator(text);
    char character =  iterator.current();
    while (character != CharacterIterator.DONE ){
      if (character == '\"') {
        result.append("\\");
        result.append("\"");
      }
      else if (character == '\'') {
        result.append("\\");
        result.append("\'");
      }
      else {
        //the char is not a special one
        //add it to the result as is
        result.append(character);
      }
      character = iterator.next();
    }
    return result.toString();
  }
}

